<?php

namespace app\factory\logic;

use app\common\exception\LogicException;
use app\model\Fence;
use app\model\Pig;
use think\facade\Db;

class PigLogic
{
    protected Pig $model;
    protected int $factory_id;

    public function __construct()
    {
        $this->model = new Pig();
        $this->factory_id = request()->factory_info['factory_id'];
    }

    /**
     * @Description 新增/编辑
     * @param int $Id
     * @param array $param
     * @time 2024/03/15 16:27:14
     * @return true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author: zj
     */
    public function addEdit(int $Id, array $param)
    {
        $fenceModel = new Fence();
        $fence = $fenceModel->field('id,out_barn_id,name')
            ->where('out_id', $param['FenceId'])
            ->where('delete_time', $fenceModel::IS_DELETE_NO)
            ->find();

        Db::startTrans();
        try {
            $exist = $this->model->checkOutIdIsExist($this->factory_id, $Id);

            if ($exist) {
                $rule = [
                    'FenceId' => 'integer|>:0',
                    'TagAddress' => 'max:255',
                    'Health' => 'require',
                    'Weight' => 'float|>:0',
                    'IsTempOut' => 'in:0,1',
                    'EntryTime' => '>:0',
                    'FeedBaseTime' => '>:0',
                    'FeedPreDayId' => '>=:0',
                    'isLoop' => 'in:0,1',
                    'Breed' => 'max:255',
                ];
                validate($rule)->check($param);

                $updateData = [];
                if ($param['FenceId']) {
                    $updateData['out_fence_id'] = $param['FenceId'];
                }
                if ($param['TagAddress']) {
                    $updateData['tag_address'] = $param['TagAddress'];
                }
                if ($param['Health']) {
                    $updateData['health'] = $param['Health'];
                }
                if ($param['Weight']) {
                    $updateData['weight'] = $param['Weight'];
                }
                if ($param['Breed']) {
                    $updateData['breed'] = $param['Breed'];
                }
                if (!empty($param['IsTempOut'])) {
                    $updateData['is_temp_out'] = $param['IsTempOut'];
                }
                if ($param['EntryTime']) {
                    $updateData['entry_time'] = $param['EntryTime'];
                }
                if ($param['ExitTime']) {
                    $updateData['exit_time'] = $param['ExitTime'];
                }
                if ($param['ExitRemark']) {
                    $updateData['exit_remark'] = $param['ExitRemark'];
                }
                if ($param['FeedBaseTime']) {
                    $updateData['feed_base_time'] = $param['FeedBaseTime'];
                }
                if ($param['FeedPreDayId']) {
                    $updateData['feed_plan_id'] = $param['FeedPreDayId'];
                }
                if ($param['isLoop']) {
                    $updateData['is_loop'] = $param['isLoop'];
                }
                $updateData['update_record_time'] = $param['RecordTime'];
                $updateData['update_time'] = request()->time();

                $this->model->where('out_id', $Id)->update($updateData);
            } else {
                $rule = [
                    'FenceId' => 'require|integer|>:0',
                    'TagAddress' => 'require|max:255',
                    'Health' => 'require|require',
                    'Weight' => 'require|float|>:0',
                    'IsTempOut' => 'require|in:0,1',
                    'EntryTime' => 'require|>:0',
                    'FeedBaseTime' => 'require|>:0',
                    'FeedPreDayId' => 'require|>=:0',
                    'isLoop' => 'require|in:0,1',
                    'Breed' => 'require|max:255',
                ];
                validate($rule)->check($param);

                $this->model->insert([
                    'out_id' => $Id,
                    'factory_id' => $this->factory_id,
                    'out_barn_id' => $fence['out_barn_id'] ?: 0,
                    'out_fence_id' => $param['FenceId'],
                    'breed' => $param['Breed'],
                    'tag_address' => $param['TagAddress'],
                    'health' => $param['Health'],
                    'weight' => $param['Weight'],
                    'is_temp_out' => $param['IsTempOut'],
                    'entry_time' => $param['EntryTime'],
                    'exit_time' => $param['ExitTime'],
                    'exit_remark' => $param['ExitRemark'],
                    'feed_base_time' => $param['FeedBaseTime'],
                    'feed_plan_id' => $param['FeedPreDayId'],
                    'is_loop' => $param['isLoop'],
                    'record_time' => $param['RecordTime'],
                    'update_record_time' => $param['RecordTime'],
                    'create_time' => request()->time(),
                    'update_time' => request()->time()
                ]);
            }

            Db::commit();
        } catch (\Exception $exception) {
            Db::rollback();
            throw new LogicException($exception->getMessage());
        }

        return true;
    }

    /**
     * @Description 生猪批次
     * @param int $PigId
     * @param string $Date
     * @time 2024/03/12 11:46:32
     * @return true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author: zj
     */
    public function batch(int $PigId, string $Date)
    {
        $exist = $this->model->checkOutIdIsExist($this->factory_id, $PigId);
        if (!$exist) {
            throw new LogicException('生猪不存在');
        }

        $this->model->where('out_id', $PigId)
            ->update([
                'batch' => $Date,
                'update_time' => request()->time()
            ]);

        return true;
    }
}